const app = getApp();
let scrollTimer = null;
// pages/user/manager/orderManage/orderManage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    onTheWayOrderVOList: [],
    complateOrderVOList: [],
    allOrderVOList: [],
    unPayOrderVOList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onChangeTab({detail: {index: 0}})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let tabIndex = this.data.tabIndex
    console.log(tabIndex)
    this.onChangeTab({detail: {index: tabIndex}})
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  orderDetail(e){
    this.stopAutoTabChange();
    wx.navigateTo({
      url: '/pages/user/order/orderDetail/orderDetail?orderNo=' + e.currentTarget.dataset.id,
    })
  },

  updateStatus(e){
    let _this = this
    const orderNo = e.currentTarget.dataset.id;
    const orderStatus = e.currentTarget.dataset.status;
    let tabIndex = _this.data.tabIndex
    app.request('/order/updateOrderStatus', 'POST', {
      orderNo: orderNo,
      orderStatus: orderStatus
    }).then(data => {
      if(data.code === 0){
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        });
        _this.onChangeTab({ detail: { index: tabIndex } });
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      console.error('服务繁忙', err);
      wx.showToast({
        title: '服务繁忙',
        icon: 'none'
      });
    });
  },

  // 定时器相关
  startAutoTabChange() {
    // 保存定时器 id
    this.autoTabTimer = setInterval(() => {
      // 调用 onChangeTab 方法
      this.onChangeTab({ detail: { index: 0 } });
    }, 2000); // 每 10 秒执行一次
  },

  stopAutoTabChange() {
    if (this.autoTabTimer) {
      clearInterval(this.autoTabTimer);
      this.autoTabTimer = null;
    }
  },

  onChangeTab(e){
    let _this = this
    let tabIndex = e.detail.index;
    _this.setData({
      tabIndex: tabIndex
    })
    let orderStatusList = [];
    if(tabIndex == 0){
      orderStatusList = ['1']
    }else if(tabIndex == 1){
      orderStatusList = ['2']
    }else if(tabIndex == 2){
      orderStatusList = ['3']
    }else if(tabIndex == 3){
      orderStatusList = ['1', '2', '3', '4', '5']
    }
    _this.selectOrderByStatus(orderStatusList).then(orderList => {
      if(tabIndex == 0){
        _this.setData({
          unPayOrderVOList: orderList
        })
      }else if(tabIndex == 1){
        _this.setData({
          onTheWayOrderVOList: orderList
        })
      }else if(tabIndex == 2){
        _this.setData({
          complateOrderVOList: orderList
        })
      }else if(tabIndex == 3){
        _this.setData({
          allOrderVOList: orderList
        })
      }
    });
    
  },

  selectOrderByStatus(orderStatusList) {
    return app.request('/order/getByOrderStatus', 'POST', { orderStatusList })
      .then(data => {
        if (data.code === 0) {
          let orderVOList = data.orderVOList;
          if(orderVOList != null){
            for(let i = 0; i < orderVOList.length; i++){
              let orderGoodsVOList = orderVOList[i].orderGoodsVOList;
              for(let j = 0; j < orderGoodsVOList.length; j++){
                orderGoodsVOList[j].goodsImg = app.getFileUrl(orderGoodsVOList[j].goodsImg)
              }
              orderVOList[i].orderGoodsVOList = orderGoodsVOList;
            }
          }
          return orderVOList; // 返回数据
        } else {
          wx.showToast({
            title: data.msg || '请求失败',
            icon: 'none'
          });
          return [];
        }
      })
      .catch(err => {
        console.error(err);
        wx.showToast({
          title: '服务繁忙',
          icon: 'none'
        });
        return [];
      });
  },


  onPageScroll(e) {
    if(scrollTimer) clearTimeout(scrollTimer);
    scrollTimer = setTimeout(() => {
      // 你的滚动处理逻辑
    }, 50);
  }
})